import roleApi from "@/api/roleApi";
import dateTime from "@/utils/dateutils";


export default {
    name: "index",
    data() {
        return {
            pickerOptions: dateTime.pickerOptions,
            value2: dateTime.value2,
            searchForm: {
                currentPage: 1,
                pageSize: 5,
            },
            tableData: [],
            total: 0,
            editDialog: false,
            formData: {},
            ids: [],
            //表单校验的独享
            formRules: {
                roleName: [
                    {required: true, message: '角色名称不能为空', trigger: 'blur'},
                ],
                roleDesc: [
                    {required: true, message: '角色描述不能为空', trigger: 'blur'},
                ],

            },
            defaultProps: {
                children: 'children',
                label: 'menuTitle'
            },
            menuTreeList: [],
            id: 0
        }
    },
    created() {
        this.searchPage();
        this.getAllTreeMenu();
    },

    methods: {
        //////////////////////业务逻辑的方法//////////////////
        /*分页条件查询*/
        async searchPage() {
            let result = await roleApi.searchPage(this.searchForm);
            console.log(result)
            this.tableData = result.data;
            this.total = result.total;
        },

        /**
         * 获得所有的树形菜单
         */

        async getAllTreeMenu() {
            this.menuTreeList = await roleApi.getAllMenuTree();
        },


        /*添加或者修改品牌*/
        addOrEdit() {
            this.$refs.form.validate(async (valid) => {
                if (valid) {
                    if (this.formData.id) {
                        await roleApi.update(this.formData);
                    } else {
                        await roleApi.add(this.formData);
                    }
                    this.searchPage();
                    this.editDialog = false;
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });


        },

        /*删除品牌*/
        async batchDelete() {
            await roleApi.batchDelete(this.ids);
            this.searchPage();
        },

        /**通过id查询*/
        async findById(id) {
            this.formData = await roleApi.findById(id);
        },

        //给角色授予权限

        async setRoleMenu() {
            //获取选择的角色id  id
            // 获取勾选的权限
            var checkedKeys = this.$refs.tree.getCheckedKeys();
            var halfCheckedKeys = this.$refs.tree.getHalfCheckedKeys();
            var allIds = checkedKeys.concat(halfCheckedKeys);
            await roleApi.setRoleMenu(this.id, allIds);
            this.id = 0;
            this.$refs.tree.setCheckedKeys([]);

        },

        /**
         * 获得角色已有的权限
         * @param value
         *
         */
        async getRoleMenu(id) {
            let result = await roleApi.getRoleMenu(id)
            this.$refs.tree.setCheckedKeys(result);
        },

        //////////////////////处理页面效果////////////////////////////
        chooseTime(value) {
            this.searchForm.startTime = value[0],
                this.searchForm.endTime = value[1]
        },


        /*点击分页效果*/
        pageChangeHandler(page) {
            this.searchForm.currentPage = page;
            this.searchPage();
        },
        /*重置查询表单*/
        resetSearchForm() {
            this.searchForm = {
                currentPage: 1,
                pageSize: 5,
            };

            this.value2 = [];
        },
        /*点击列表中的点击删除*/
        itemBtnClick(row) {
            this.$refs.table.clearSelection();
            this.$refs.table.toggleRowSelection(row)
            this.ids = [];
            this.ids.push(row.id);
        },
        /*勾选框发生改变*/
        selectChange(selection) {
            console.log(selection)
            this.ids = [];
            var map = selection.map(item => item.id);
            map.forEach(item => {
                this.ids.push(item)
            })
        },
        /*点击批量删除*/
        doBatchDelete() {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //做删除的功能
                this.batchDelete();
            }).catch(() => {
                //取消的功能
                this.$refs.table.clearSelection();
                this.ids = []
            });
        },

        /**
         * 取消删除
         */
        batchDeleteCancel() {
            this.$refs.table.clearSelection();
        },

        /**
         * 点击每一条角色时触发
         */
        tableRowClick(row) {
            this.id = row.id;
            this.getRoleMenu(this.id);
        },

    }
}
